﻿#include <iostream>
#include <string.h>

using namespace std;

int main()
{
    system("chcp 65001");
    FILE *fp_r = nullptr;
    FILE *fp_w = nullptr;
    string path_r = "./suzie_qcif_176x144.yuv";
    string path_w = "./suzie_qcif_176x144_w.yuv";

    fp_r = fopen(path_r.c_str(), "rb");
    if (!fp_r) {
        cout << "打开文件失败 "
             << path_r
             << endl;
        return 0;
    }

    fseek(fp_r, 0, SEEK_END);
    int yuv_len = ftell(fp_r);
    cout << "func:" << __func__
         << " line:" << __LINE__
         << " yuv_len:" << yuv_len
         << endl;

    fseek(fp_r, 0, SEEK_SET);

    char *buffer = new char[yuv_len + 1];
    memset(buffer, 0, yuv_len);

    size_t len_r = fread(buffer, yuv_len, 1, fp_r);
    fclose(fp_r);

    if (len_r <= 0) {
        cout << "读取文件失败 "
             << path_r
             << " len_r:" << len_r
             << endl;
    }

    fp_w = fopen(path_w.c_str(), "w+");
    if (!fp_w) {
        return 0;
    }

    size_t len_w = fwrite(buffer, yuv_len, 1, fp_w);

    if (len_w == 0) {
        cout << "func:" << __func__
             << " line:" << __LINE__
             << " len_w:" << len_w
             << endl;
    }

    fclose(fp_w);

    cout << "func:" << __func__
         << " line:" << __LINE__
         << " 写文件成功 " << path_w
         << endl;

    return 0;
}
